package com.example.simplenet;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ButtonFragment extends Fragment {

	private FragmentManager mFragmentManager;
	private Button mStartButton;
	private Button mPauseButton;
	private Button mStopButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.buttons_fragment, container,
				false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		mFragmentManager = getFragmentManager();
		mStartButton =  (Button) getView().findViewById(
				R.id.startService);
		mPauseButton =  (Button) getView().findViewById(
				R.id.pauseService);
		mStopButton =  (Button) getView().findViewById(
				R.id.stopService);
		Button buildingsButton = (Button) getView().findViewById(
				R.id.buildingsButton);
		Button researchesButton = (Button) getView().findViewById(
				R.id.researchesButton);
		Button hireButton = (Button) getView().findViewById(R.id.hireButton);
		Button abandonedButton = (Button) getView().findViewById(
				R.id.abandonedButton);
		Button missionButton = (Button) getView().findViewById(
				R.id.missionsButtton);
		Button armyButton = (Button) getView().findViewById(R.id.armyButton);
		Button graphButton = (Button) getView().findViewById(R.id.graphButton);

		mStartButton.setEnabled(!ConnectionMan.isServiceRunning());
		mStartButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).startAndBind();
			}
		});
		mPauseButton.setEnabled(ConnectionMan.isServiceRunning());
		mPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).pauseService();
			}
		});
		mStopButton.setEnabled(ConnectionMan.isServiceRunning());
		mStopButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).stopService();
			}
		});
		buildingsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				BuildingFragment buildingFragment = new BuildingFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("function", BuildingFragment.BUILDINGS_FUN);
				bundle.putInt("page", 1);
				buildingFragment.setArguments(bundle);
				fragmentTransaction.replace(R.id.containerFrameLayout,
						buildingFragment, "BuildingFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		researchesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				BuildingFragment researchesFragment = new BuildingFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("function", BuildingFragment.RESEARCHES_FUN);
				bundle.putInt("page", 1);
				researchesFragment.setArguments(bundle);
				fragmentTransaction.replace(R.id.containerFrameLayout,
						researchesFragment, "BuildingFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		hireButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				((MainActivity)getActivity()).sendMessageToService(ConnectionMan.MSG_HIRE, null);
			}
		});
		abandonedButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				AbandonedFragment abandonedFragment = new AbandonedFragment();
				fragmentTransaction.replace(R.id.containerFrameLayout,
						abandonedFragment, "AbandonedFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		armyButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				ArmyFragment armyFragment = new ArmyFragment();
				Bundle bundle = new Bundle();
				bundle.putBoolean("on", ((MainActivity)getActivity()).mAttacksSheduled);
				armyFragment.setArguments(bundle);
				fragmentTransaction.replace(R.id.containerFrameLayout,
						armyFragment, "ArmyFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		missionButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				((MainActivity)getActivity()).getMissions();
				
				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				MissionsFragment missionsFragment = new MissionsFragment();
				fragmentTransaction.replace(R.id.containerFrameLayout,
						missionsFragment, "MissionsFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		graphButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FragmentTransaction fragmentTransaction = mFragmentManager
						.beginTransaction();

				GraphFragment graphFragment = new GraphFragment();
				fragmentTransaction.replace(R.id.containerFrameLayout,
						graphFragment, "GraphFragment");
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
		super.onActivityCreated(savedInstanceState);
	}

	public void updateButtons(boolean running) {
		mStartButton.setEnabled(!running);
		mPauseButton.setEnabled(running);
		mPauseButton.setText(getString(ConnectionMan.isServicePaused() ? R.string.continue_service : R.string.pause_service));
		mStopButton.setEnabled(running);
	}

}
